package com.sa.web.behind.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sa.web.bean.AdminBean;
import com.sa.web.dao.service.AdminInfoDao;
import com.sa.web.util.Util;

/**
 * 后台管理首页
 */

public class AdminIndexAction extends ActionSupport{
	/**序列*/ 
	private static final long serialVersionUID = 1L;
	
	private AdminBean adminBean;
	
	private String checkword;
	
	private AdminInfoDao adminInfoDao;
	
	public void validateInit() throws Exception{
		this.clearFieldErrors();
		if(adminBean == null){
			this.addFieldError("nologin", "");
			return;
		}
		if(Util.isEmpty(adminBean.getAdminName())){
			this.addFieldError("username", "请输入用户名");
		}
		
		if(Util.isEmpty(adminBean.getPassword())){
			this.addFieldError("password", "请输入密码");
		}
		
		if(Util.isEmpty(checkword)){
			this.addFieldError("checkword", "请输入验证码");
		}else{
			ActionContext context = ActionContext.getContext();  
			Map<String , Object> session = context.getSession();
			String sessionCheckWord = (String)session.get("RANDOMVALIDATECODEKEY");
			if(!sessionCheckWord.equalsIgnoreCase(checkword)){
				this.addFieldError("checkword", "验证码不正确");
			}
		}
		
	}
	
	public String init() throws Exception{	
		AdminBean admin = adminInfoDao.adminLogin(adminBean.getAdminName());
		if(admin != null){
			 ActionContext context = ActionContext.getContext();  
			 Map<String , Object> session = context.getSession();
			 session.put("Manager", admin);
			 return SUCCESS;
		}
		return INPUT;
	}
	
	public String adminLogin() throws Exception{
		return LOGIN;
	}

	public String getCheckword() {
		return checkword;
	}

	public void setCheckword(String checkword) {
		this.checkword = checkword;
	}

	public AdminBean getAdminBean() {
		return adminBean;
	}

	public void setAdminBean(AdminBean adminBean) {
		this.adminBean = adminBean;
	}

	public AdminInfoDao getAdminInfoDao() {
		return adminInfoDao;
	}

	public void setAdminInfoDao(AdminInfoDao adminInfoDao) {
		this.adminInfoDao = adminInfoDao;
	}

}
